var express = require('express');
var router = express.Router();
var { home,
    area,
    order } = require('../model/model');
// /* GET homem page. */
const AlipayFormData = require('../node_modules/alipay-sdk/lib/form');

const AlipaySdk = require('alipay-sdk').default;


// 初始化支付宝 SDK
const alipaySdk = new AlipaySdk({
    appId: '9021000140684897',
    signType: 'RSA2',
    gateway: 'https://openapi-sandbox.dl.alipaydev.com/gateway.do',
    alipayPublicKey: 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAibPTM8Nd0R1Txk3nGt4DWFWZrwzTGlklbWUFWl8vdSG7VimfrgeUYau/EF1gOGkY50fR89pSJH33jNsXKkq2c5QLRgs3OKJmaWlw6j6bnC6gHL+QzpaqamagLe5zui0G6QRuxoqUXxJvcb6CbLPu1GEhZx6YqPX5e8IBgn5IdJRAZIu3dsHJ80PgwrcklV/Esj+3c82tX3QS+Dq+8CY/II13GL2+J9nBcALH2G3GGzKe4NSGNDSfy255UyRYl8ZaMxpBB7XZNrC5UGmHr9bRh3TO1iIDJQBqJQee2AjnxKRJrWzCKUQ4bvEKAm5/MFRs2QtbXPksh1Z48SIV2dhaRwIDAQAB',
    privateKey: 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCJs9Mzw13RHVPGTeca3gNYVZmvDNMaWSVtZQVaXy91IbtWKZ+uB5Rhq78QXWA4aRjnR9Hz2lIkffeM2xcqSrZzlAtGCzc4omZpaXDqPpucLqAcv5DOlqpqZqAt7nO6LQbpBG7GipRfEm9xvoJss+7UYSFnHpio9fl7wgGCfkh0lEBki7d2wcnzQ+DCtySVX8SyP7dzza1fdBL4Or7wJj8gjXcYvb4n2cFwAsfYbcYbMp7g1IY0NJ/LbnlTJFiXxlozGkEHtdk2sLlQaYev1tGHdM7WIgMlAGolB57YCOfEpEmtbMIpRDhu8QoCbn8wVGzZC1tc+SyHVnjxIhXZ2FpHAgMBAAECggEAOcZqQdihf0CHyrca1On7i093CLwfwnlOu8Rb55vyRHnfqPo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D6uWb4sHi4+9ieXsYuQol4xEQsI0Z3DY0pwMr/4guytnKxelk7leJlBj2ukpxtPqKCaGDXzKvTQWf72jFp3RxYm'
});
router.get('/housexq', async (req, res) => {
    let id = req.query.id
    let data = await home.find({ _id: id }).populate("c_id").populate('u_id')
    res.send({
        code: 200,
        data: data
    })
})

router.post("/pcpay", async (req, res) => {
    let { price, fname } = req.query
    const formData = new AlipayFormData()
    formData.setMethod('get');
    // 通过addField增加参数
    // 在用户支付完成之后，支付宝服务器会根据传入的notify_url,以post请求的形式将支付结果作为参数通知到商户系统
    // 支付成功或失败都返回
    formData.addField('returnUrl', "http://localhost:5173/home/paysuccess");
    formData.addField('bizContent', {
        out_trade_no: new Date(),//订单号 64个字符以内 可包含字母、数字、下划线、且不能重复
        product_code: "FAST_INSTANT_TRADE_PAY",//销售产品码，与支付宝签约的产品码名称，仅支持FAST_INSTANT_TRADE_PAY
        total_amount: price,// 订单总金额
        subject: '君假酒店（千山城规站店）',// 订单描述
        body: fname,// 商品描述
    })
    // 返回promise
    const result = await alipaySdk.exec(
        'alipay.trade.wap.pay',//统一收单下单并支付页面接口,
        {},//api请求的参数（包含 “公共请求参数”和“业务参数”）
        { formData: formData }// result为可以跳转支付链接的url
    )
    // res.json({ url: result })
    res.send({
        code: 200,
        result
    })

})
router.get('/addhouseorder', async (req, res) => {
    let { id, entertime, leavetime, days, entername, phone, price, pay, peopleNum } = req.query
    let data = await home.find({ _id: id }).populate('c_id').populate('u_id')
    await order.create({
        entertime: entertime,//入住时间
        leavetime: leavetime,//退房时间
        days: days,//入住天数
        entername: entername,//入住人姓名
        phone: phone,//联系方式
        price: price,//总价格
        name: data[0].name,//房间类型
        address: data[0].address,//地址
        roomNum: 1,//房间数
        bedNum: data[0].bedNum,//床数量
        visitorNum: peopleNum,//入住人数
        pay: pay,//支付方式
        paystatus: false
    })
    res.send({
        code: 200
    })
})
router.get('/houseorder', async (req, res) => {
    let data = await order.find()
    res.send({
        code: 200,
        data: data
    })
})
router.get('/orderxq', async (req, res) => {
    let id = req.query.id
    let data = await order.find({ _id: id })
    res.send({
        code: 200,
        data: data
    })
})





module.exports = router;